package cn.csc.module.pay.mq.consumer;

import cn.csc.framework.common.util.json.JsonUtils;
import cn.csc.module.pay.message.JzbOrderMessage;
import cn.csc.module.pay.service.jzborder.JzbOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;

import javax.annotation.Resource;
import java.util.function.Consumer;

import static cn.csc.framework.common.util.json.JsonUtils.toJsonString;

/**
 * 见证宝订单消息消费
 * 与配置中的bindings下的channel名：jzbOrder-in-0 的前半段对应
 * @author liuchuang
 * @version 1.0
 * @date 2024/1/30 17:34
 * @modify
 */
@Slf4j
@Configuration
public class JzbOrderConsumer {

    @Resource
    @Lazy // 循环依赖（自己依赖自己），避免报错
    private JzbOrderConsumer jzbOrderConsumer;
    @Resource
    JzbOrderService jzbOrderService;


    /**
     * 见证宝订单消费
     * message： the input argument
     */
    @Bean
    public Consumer<JzbOrderMessage> jzbOrder(){
        return message -> {
            log.info("JzbOrderConsumer见证宝订单消费开始: {}", toJsonString(message));
            try {
                // 如果订单未支付的话，将订单设为取消状态
                jzbOrderConsumer.executeNotifyAsync(message);
            } catch (Exception e) {
                log.error("JzbOrderConsumer见证宝订单消息消费失败:{}", message, e);
            }
        };
    }


    /**
     * @Async默认使用的ForkJoinPool.commonPool
     */
    @Async("defaultTaskExecutor")
    public void executeNotifyAsync(JzbOrderMessage message) {
        log.info("JzbOrderConsumer.executeNotifyAsync: {}", message);
        String type = message.getTypeId();
        if ("1".equals(type)) {
            jzbOrderService.createJzbFundDetails(message);
        } else if ("2".equals(type)) {

        } else {
            log.error("-------------[冻结撤销消息类型有误]-------{}", JsonUtils.toJsonString(message));
        }
    }



}
